package com.example.viewtest.FragmentGroup;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.viewtest.AdapterGroup.Adapter_FragmentOne;
import com.example.viewtest.AdapterGroup.HeaderBottomAdapter;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class FragmentOne extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private List<Bean> list = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private Adapter_FragmentOne adapterFragmentOne;
    LinearLayoutManager layoutManager;

    public FragmentOne() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentOne.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentOne newInstance(String param1, String param2) {
        FragmentOne fragment = new FragmentOne();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        mRecyclerView = view.findViewById(R.id.recyclerview_fragmentOne);
        initRecyclerViewData();
        //创建和设置布局管理器
        layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        //设置适配器
        mRecyclerView.setAdapter(adapterFragmentOne = new Adapter_FragmentOne(requireContext(), list));

        DividerItemDecoration itemDecoration = new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL);
        Drawable drawable = getResources().getDrawable(R.drawable.draw_divider);
        itemDecoration.setDrawable(drawable);
        mRecyclerView.addItemDecoration(itemDecoration);

        return view;
    }

    public void initRecyclerViewData() {
        Random random = new Random();
        for (int i = 0; i < 30; i++) {
            Bean bean = new Bean();
            bean.setName("Item" + i);
//            bean.setType((random.nextInt()*2)+3);//[3,5)
//            Log.d("随机数生成监控","-----: "+bean.getType());
            switch (i % 4) {
                case 1:
                    bean.setImageId(R.drawable.game1);
                    break;
                case 2:
                    bean.setImageId(R.drawable.game2);
                    break;
                case 3:
                    bean.setImageId(R.drawable.game3);
                    break;
                case 0:
                    bean.setImageId(R.drawable.landscape5);
                    break;
                default:
            }
            list.add(bean);
        }
    }
}